package com.itheima.dubbo.mysql.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.commons.pojo.domain.User;
import com.itheima.dubbo.interfaces.UserApi;
import com.itheima.dubbo.mysql.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author yaohu
 * @date 2021/12/3
 * @Version 1.0
 * @describe
 */
@Service
public class UserApiImpl implements UserApi {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findUserByPhoneNum(String phoneNum) {
        return userMapper.selectOne(Wrappers.lambdaQuery(User.class).eq(User::getMobile, phoneNum));
    }

    @Override
    public User addUser(User user) {
        userMapper.insert(user);
        return user;
    }

    /**
     * 更新手机号
     *
     * @param userMobile 旧号码
     * @param phone      新号码
     */
    @Override
    public void updatePhone(String userMobile, String phone) {
        userMapper.update(User.builder().mobile(phone).build(),
                Wrappers.lambdaQuery(User.class).eq(User::getMobile, userMobile));
    }
}
